package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.MdmRailMapper;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.MdmGeofenceEntity;
import com.kaili.basic.pojo.entity.MdmRailEntity;
import com.kaili.basic.pojo.entity.MdmRailStrategyEntity;
import com.kaili.basic.pojo.entity.MdmTimefenceEntity;
import com.kaili.basic.service.IMdmRailService;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
*  service实现类
* @author:nigang
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class MdmRailServiceImpl implements IMdmRailService {
    @Autowired
    MdmRailMapper mdmRailMapper;

    //新增地理围栏
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int addGeofence(MdmGeofenceAddDto dto) {
        MdmGeofenceEntity entity = new MdmGeofenceEntity();
        BeanUtils.copyProperties(dto, entity);
        Integer id = mdmRailMapper.getMaxId();
        entity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId());
        entity.setId(id);
        mdmRailMapper.addGeofence(entity);
        if(dto.getStrategyIds()!=null) {
            for (Integer strategyId : dto.getStrategyIds()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(id);
                railStrategy.setStrategyId(strategyId);
                railStrategy.setRailType(2);
                mdmRailMapper.addRailStrategy(railStrategy);
            }
        }
        return 1;
    }

    //新增时间围栏
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int addTimefence(MdmTimefenceAddDto dto) {
        MdmTimefenceEntity entity = new MdmTimefenceEntity();
        BeanUtils.copyProperties(dto, entity);
        String dateStr = "";
        if(dto.getExceptDateList()!=null&&dto.getExceptDateList().length>0) {
            for (Date date : dto.getExceptDateList()) {
                dateStr += new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE).format(date) + ";";
            }
        }
        entity.setExceptDate(dateStr);
        Integer id = mdmRailMapper.getMaxId();
        entity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId());
        entity.setId(id);
        mdmRailMapper.addTimefence(entity);
        if(dto.getStrategyIds()!=null) {
            for (Integer strategyId : dto.getStrategyIds()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(id);
                railStrategy.setStrategyId(strategyId);
                railStrategy.setRailType(1);
                mdmRailMapper.addRailStrategy(railStrategy);
            }
        }
        return 1;
    }

    //新增围栏策略
    @Override
    public int addRailStrategy(MdmRailStrategyEntity entity) {
        mdmRailMapper.addRailStrategy(entity);
        return 1;
    }

    //根据围栏id删除策略
    @Override
    public int deleteRailStrategy(Integer railId) {
        mdmRailMapper.deleteRailStrategy(railId);
        return 1;
    }

    //根据围栏id删除策略
    @Override
    public int deleteStrategyRail(Integer strategyId) {
        mdmRailMapper.deleteStrategyRail(strategyId);
        return 1;
    }



    //删除围栏
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteRail(Integer id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy",LoginDetailUtils.getCurrentEmployeeId());
        map.put("id",id);
        mdmRailMapper.deleteGeofence(map);
        mdmRailMapper.deleteTimefence(map);
        return 1;
    }

    //删除地理围栏
    @Override
    public int deleteGeofence(Integer id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy",LoginDetailUtils.getCurrentEmployeeId());
        map.put("id",id);
        mdmRailMapper.deleteGeofence(map);
        return 1;
    }

    //删除时间围栏
    @Override
    public int deleteTimefence(Integer id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy",LoginDetailUtils.getCurrentEmployeeId());
        map.put("id",id);
        mdmRailMapper.deleteTimefence(map);
        return 1;
    }

    //修改地理围栏
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateGeofence(MdmGeofenceUpdateDto dto) {
        MdmGeofenceEntity entity = new MdmGeofenceEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setModifyBy(LoginDetailUtils.getCurrentEmployeeId());
        mdmRailMapper.deleteRailStrategy(dto.getId());
        mdmRailMapper.updateGeofence(entity);
        if(dto.getStrategyIds()!=null) {
            for (Integer strategyId : dto.getStrategyIds()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(dto.getId());
                railStrategy.setStrategyId(strategyId);
                railStrategy.setRailType(2);
                mdmRailMapper.addRailStrategy(railStrategy);
            }
        }
        return 1;
    }

    //修改时间围栏
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateTimefence(MdmTimefenceUpdateDto dto) {
        MdmTimefenceEntity entity = new MdmTimefenceEntity();
        BeanUtils.copyProperties(dto, entity);
        String dateStr = "";
        if(dto.getExceptDateList()!=null&&dto.getExceptDateList().length>0) {
            for (Date date : dto.getExceptDateList()) {
                dateStr += new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE).format(date) + ";";
            }
        }
        entity.setExceptDate(dateStr);
        entity.setModifyBy(LoginDetailUtils.getCurrentEmployeeId());
        mdmRailMapper.deleteRailStrategy(dto.getId());
        mdmRailMapper.updateTimefence(entity);
        if(dto.getStrategyIds()!=null) {
            for (Integer strategyId : dto.getStrategyIds()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(dto.getId());
                railStrategy.setStrategyId(strategyId);
                railStrategy.setRailType(1);
                mdmRailMapper.addRailStrategy(railStrategy);
            }
        }
        return 1;
    }

    //id查询地理围栏
    @Override
    public MdmGeofenceEntity queryGeofenceById(Integer id) {
        return mdmRailMapper.queryGeofenceById(id);
    }

    //name查询地理围栏
    @Override
    public MdmGeofenceEntity queryGeofenceByName(String name) {
        return mdmRailMapper.queryGeofenceByName(name);
    }

    //查询所有地理围栏
    @Override
    public List<MdmGeofenceEntity> queryGeofenceAll() {
        return mdmRailMapper.queryGeofenceAll();
    }

    //id查询时间围栏
    @Override
    public MdmTimefenceEntity queryTimefenceById(Integer id) throws Exception {
        MdmTimefenceEntity entity=mdmRailMapper.queryTimefenceById(id);
        if(entity!=null&&entity.getExceptDate()!=null&&!"".equals(entity.getExceptDate())){
            String[] strs=entity.getExceptDate().split(";");
            Date[] dates=new Date[strs.length];
            for(int i=0;i<strs.length;i++){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = format.parse(strs[i]);
                dates[i]=date;
            }
            entity.setExceptDateList(dates);
        }
        return entity;
    }

    //name查询时间围栏
    @Override
    public MdmTimefenceEntity queryTimefenceByName(String name) throws Exception {
        MdmTimefenceEntity entity=mdmRailMapper.queryTimefenceByName(name);
        if(entity!=null&&entity.getExceptDate()!=null&&!"".equals(entity.getExceptDate())){
            String[] strs=entity.getExceptDate().split(";");
            Date[] dates=new Date[strs.length];
            for(int i=0;i<strs.length;i++){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = format.parse(strs[i]);
                dates[i]=date;
            }
            entity.setExceptDateList(dates);
        }
        return entity;
    }

    //查询所有时间围栏
    @Override
    public List<MdmTimefenceEntity> queryTimefenceAll() {
        return mdmRailMapper.queryTimefenceAll();
    }

    //围栏id查询策略集合
    @Override
    public List<MdmRailStrategyEntity> getRailStrategyList(Integer id) {
        return mdmRailMapper.getRailStrategyList(id);
    }

    //查询围栏的最大id
    @Override
    public Integer getMaxId() {
        return mdmRailMapper.getMaxId();
    }

    //分页查询围栏管理
    @Override
    public Page<MdmRailEntity> queryRail(Page<MdmRailEntity> page, MdmRailQueryDto dto) {
        List<MdmRailEntity> entities = mdmRailMapper.queryRail(page,dto);
        return page.setRecords(entities);
    }
}
